package pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.factory;

import org.apache.log4j.Logger;

import pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.GlpRptaConsultaVenta;
import pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.glp.GlpRegistroVentaDetalle;
import pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.glp.GlpRegistroVentaDocAnti;
import pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.glp.GlpRegistroVentaOpBonus;
import pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.glp.GlpRegistroVentaPrincipal;
import pe.com.pgn.consolidadorventas.servidorcentral.ws.domain.glp.GlpRegistroVentaSalesCard;

public class GlpParserLinea {

	static Logger logger = Logger.getLogger(GlpParserLinea.class);
	
	//Aqui parseamos solo la linea principal
	public static GlpRegistroVentaPrincipal parsearLineaPrincipal(String valores[]){
		
		GlpRegistroVentaPrincipal registro = null;

		int i = 1;
		if(valores.length>0){
			registro = new GlpRegistroVentaPrincipal();
			try{
				registro.setDocTypeId(valores[i]);i=i+1;
				registro.setNbrDocument(valores[i]);i=i+1;
				registro.setSiteId(valores[i]);i=i+1;
				
				registro.setTypePaymentId(valores[i]);i=i+1;
				registro.setStkInventory(valores[i]);i=i+1;
				
				registro.setDocStatusId(valores[i]);i=i+1;
				registro.setTermId(valores[i]);i=i+1;
				registro.setSalesPerId(valores[i]);i=i+1;
				registro.setUserId(valores[i]);i=i+1;
				registro.setDateDoc(valores[i]);i=i+1;
				registro.setDateProceSales(valores[i]);i=i+1;
				registro.setPerPost(valores[i]);i=i+1;	
				registro.setTotalPen(valores[i]);i=i+1;
				registro.setSubTotalPen(valores[i]);i=i+1;
				registro.setTaxIgvPen(valores[i]);i=i+1;
				registro.setTotCashPen(valores[i]);i=i+1;
					
				registro.setCustIdss(valores[i]);i=i+1;
				
				registro.setTaxRegnBr(valores[i]);i=i+1;
				registro.setPlaca(valores[i]);i=i+1;
				registro.setNbrBonus(valores[i]);i=i+1;
				
				registro.setSalesPoint(valores[i]);i=i+1;
				registro.setKilometraje(valores[i]);i=i+1;
				registro.setBusinessType(valores[i]);i=i+1;
				registro.setUser9(valores[i]);i=i+1;
				registro.setCustName(valores[i]);i=i+1;
				registro.setFilaVenDet(valores[i]);i=i+1;
				registro.setFilaVenSal(valores[i]);i=i+1;
				registro.setFilaVenDoc(valores[i]);i=i+1;
				registro.setFilaVenBon(valores[i]);i=i+1;
				registro.setChkVenDet(valores[i]);i=i+1;
				registro.setChkVenSal(valores[i]);i=i+1;	
				
			}catch(ArrayIndexOutOfBoundsException e){
				logger.error(e.getMessage());
				registro = null;
			}	
		}
		
		return registro;
	}
	
	//Linea de detalle
	public static GlpRegistroVentaDetalle parsearLineaDetalle(String valores[]){
		
		GlpRegistroVentaDetalle detalle = null;
		
		int i = 1;
		if(valores.length>0){		
			detalle = new GlpRegistroVentaDetalle();
			
			try{
				detalle.setDocTypeId(valores[i]);i=i+1;
				detalle.setNbrDocument(valores[i]);i=i+1;
				detalle.setSiteId(valores[i]);i=i+1;
				detalle.setSequence(valores[i]);i=i+1;
				detalle.setInvtidsku(valores[i]);i=i+1;
				detalle.setDocStatusId(valores[i]);i=i+1;
				
				detalle.setDateDoc(valores[i]);i=i+1;
				detalle.setDateProceSales(valores[i]);i=i+1;
				
				detalle.setPerPost(valores[i]);i=i+1;
				detalle.setNbrTurn(valores[i]);i=i+1;
				detalle.setNbrSide(valores[i]);i=i+1;
				detalle.setPorcentTaxIgv(valores[i]);i=i+1;
				detalle.setTotalPen(valores[i]);i=i+1;
				detalle.setTaxPen(valores[i]);i=i+1;
				detalle.setStkItem(valores[i]);i=i+1;
				detalle.setSlsPrice(valores[i]);i=i+1;
				detalle.setSlsPriceSale(valores[i]);i=i+1;
				detalle.setStdCostPen(valores[i]);i=i+1;
				detalle.setDescrInventory(valores[i]);i=i+1;
				detalle.setQty(valores[i]);i=i+1;
				detalle.setUserId(valores[i]);i=i+1;
				
			}catch(ArrayIndexOutOfBoundsException e){
				logger.error(e.getMessage());
				detalle = null;
			}	
		}
		
		return detalle;
	}
	
	public static GlpRegistroVentaSalesCard parsearLineaSalesCard(String valores[]){
		
		GlpRegistroVentaSalesCard salesCard = null;
		int i = 1;
		if(valores.length>0){
			salesCard = new GlpRegistroVentaSalesCard();
			try{		
				salesCard.setDocTypeId(valores[i]);i=i+1;
				salesCard.setNbrDocument(valores[i]);i=i+1;
				salesCard.setSiteId(valores[i]);i=i+1;
				salesCard.setSequence(valores[i]);i=i+1;
				salesCard.setCardId(valores[i]);i=i+1;
				salesCard.setNbrCard(valores[i]);i=i+1;
				salesCard.setTotCardPen(valores[i]);i=i+1;
				salesCard.setDateProceSales(valores[i]);		
			}catch(ArrayIndexOutOfBoundsException e){
				logger.error(e.getMessage());
				salesCard = null;	
			}
		}
		
		return salesCard;
	}
	
	public static GlpRegistroVentaDocAnti parsearLineaDocAnti(String valores[]){
		GlpRegistroVentaDocAnti docAnti = null;
		int i = 1;
		if(valores.length>0){
			docAnti = new GlpRegistroVentaDocAnti();
			docAnti.setDocTypeId(valores[i]);i=i+1;
			docAnti.setNbrDocument(valores[i]);i=i+1;
			docAnti.setSiteId(valores[i]);i=i+1;
			docAnti.setDateProce(valores[i]);i=i+1;
		}
		return docAnti;
	}
	
	public static GlpRegistroVentaOpBonus parsearLineaOpBonus(String valores[]){
		GlpRegistroVentaOpBonus opBonus = null;
		int i = 1;
		if(valores.length>0){
			opBonus = new GlpRegistroVentaOpBonus();
			opBonus.setDocTypeId(valores[i]);i=i+1;
			opBonus.setNbrDocument(valores[i]);i=i+1;
			opBonus.setSiteId(valores[i]);i=i+1;
			opBonus.setCustIdss(valores[i]);i=i+1;
			opBonus.setNbrBonus(valores[i]);i=i+1;
			opBonus.setDateProceSales(valores[i]);i=i+1;
		}
		return opBonus;
	}
	
	public static void asignarValoresVentas(GlpRptaConsultaVenta resultadoOperacion,String valores[]){
		try{
			resultadoOperacion.setCantidadVentas(Integer.parseInt(valores[1].trim()));
		}catch(Exception e){
			logger.error("Error convirtiendo el valor de numero de ventas");
		}
		
		try{
			resultadoOperacion.setVentaTotal(valores[2].trim());				
		}catch(Exception e){
			logger.error("Error convirtiendo el total de ventas");
		}
		
	}
}